package com.phy.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class UpdateMap {
	DocumentBuilderFactory factory;
	DocumentBuilder builder;
	Document doc;

	public UpdateMap() {
		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		}
		InputStream is=UpdateMap.class.getResourceAsStream("/com/phy/core/map.xml");
		try {
			doc = builder.parse(is);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 添加节点
	public void addNode(String fileName,String TextValueOne, String TextValueTwo,
			String FatherLabel, String LabelOne, String LabelTwo) {
		Text textseg;
		Element sys = doc.createElement(FatherLabel);

		Element abbr = doc.createElement(LabelOne);
		textseg = doc.createTextNode(TextValueOne);
		abbr.appendChild(textseg);
		sys.appendChild(abbr);

		Element keyword = doc.createElement(LabelTwo);
		textseg = doc.createTextNode(TextValueTwo);
		keyword.appendChild(textseg);
		sys.appendChild(keyword);

		NodeList list=doc.getElementsByTagName("mapping");
		Node root=list.item(0);
		//root.normalize();
		root.appendChild(sys);
		//打印输出xml文件
		output(doc);
		//保存文件
		saveXml(fileName,doc);
	}
	
	 public static void saveXml(String fileName, Document doc) {//将Document输出到文件
		 	File file=new File(fileName).getAbsoluteFile();
		 	if(file.exists()){
		 		System.out.println("文件存在。在"+file.getName()+"\n"+file.getParent());
		 	}
		 	else{
		 		System.out.println("文件不存在。不在"+file.getName()+"\n"+file.getParent());
		 	}
	        TransformerFactory transFactory=TransformerFactory.newInstance();
	        try {
	            Transformer transformer = transFactory.newTransformer();
	            transformer.setOutputProperty("indent", "yes");

	            DOMSource source=new DOMSource();
	            source.setNode(doc);
	            StreamResult result=new StreamResult();
	            result.setOutputStream(new FileOutputStream(file));
	            
	            transformer.transform(source, result);
	        } catch (TransformerConfigurationException e) {
	            e.printStackTrace();
	        } catch (TransformerException e) {
	            e.printStackTrace();
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        }   
	    }
	 public static void output(Node node) {//将node的XML字符串输出到控制台
	        TransformerFactory transFactory=TransformerFactory.newInstance();
	        try {
	            Transformer transformer = transFactory.newTransformer();
	            transformer.setOutputProperty("encoding", "utf-8");
	            transformer.setOutputProperty("indent", "yes");

	            DOMSource source=new DOMSource();
	            source.setNode(node);
	            StreamResult result=new StreamResult();
	            result.setOutputStream(System.out);
	            
	            transformer.transform(source, result);
	        } catch (TransformerConfigurationException e) {
	            e.printStackTrace();
	        } catch (TransformerException e) {
	            e.printStackTrace();
	        }   
	    }
}
